<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Leave_model extends CI_Model
{
	public function __construct(){
		parent::__construct();
	}
	
	public function insert_user_leave($data,$dates)
	{		
		$this->db->insert('user_leave', $data);
		$insert_id = $this->db->insert_id();
		
		foreach($dates as $date){			
			$this->db->insert('user_leave_days',array('user_leave_id'=>$insert_id,'date'=>$date,'status'=>'pending'));
		}		
		$this->db->close();
	}
	
	public function get_all_leaves($status = NULL)
	{		
	$this->db->select('*');
	$this->db->from('user_leave');
	$this->db->where('user_leave.user_id',$this->session->userdata('user_id'));	
	if($status){
	$this->db->where('user_leave.status',$status);	
	}	
	$this->db->order_by('id','DESC');	
	
	/*for pagination*/
	/*if(isset($limit)){
	$this->db->limit($limit, $start);	
	}*/		
	return  $this->db->get();	
	$this->db->close();	
	}
	
	public function get_leave_dates($id){
		$this->db->select('*');
		$this->db->from('user_leave_days');
		$this->db->where('user_leave_id',$id);
		return $this->db->get()->result();
		$this->db->close();
	}
	
	public function total_leaves()
	{
		$this->db->select('paid_leave');
		$this->db->from('salary_account');
		$this->db->where('user_id',$this->session->userdata('user_id'));
		$query = $this->db->get()->row();
		if($query){
		return $query->paid_leave;
		}else{
			return 0;
		}
		$this->db->close();
		//return $get = $this->db->get_where('user_leave', array('user_id' => $this->session->userdata('user_id')))->num_rows();	
	}
		
	public function total_leave_granted($user_id = NULL)
	{
		$id = ($user_id) ? $user_id : $this->session->userdata('user_id');
		$this->db->select('user_leave_days.status');
		$this->db->from('user_leave');
		$this->db->where('user_leave.user_id',$id);		
		$this->db->join('user_leave_days','user_leave_days.id  = user_leave.id');
		$this->db->where('user_leave_days.status','paid');
		return $this->db->get()->num_rows();
		$this->db-close();		
	}
	
	public function leave_remaining()
	{
		return ($this->total_leaves() - $this->total_leave_granted());		 
	}
		
	public function get_pending_leaves($user_id = NULL)
	{		
	$id = ($user_id) ? $user_id : $this->session->userdata('user_id');
	$this->db->select('user_leave.*,leave_status.*');
	$this->db->from('user_leave');
	$this->db->where('user_leave.user_id',$this->session->userdata('user_id'));
	//$this->db->join('leave_status','leave_status.user_leave_id=>user_leave.id');
	//$this->db->where('leave_status.status','pending');
	return  $this->db->get()->result();
	$this->db->close();	
	//return $get = $this->db->get_where('user_leave', array('user_id' => $this->session->userdata('user_id'),'status'=>'pending'))->result();
	}
	
	public function get_leave_status()
	{
		/*$this->db->select('*');
		$this->db->from('user_leave');
		$this->db->where('user_id =', $this->session->userdata('user_id'));
		$this->db->where('status !=', 'pending');
		return $this->db->get()->result();*/
		
		//return $get = $this->db->get_where('user_leave', array('user_id' => $this->session->userdata('user_id'),'status'=>'pending'))->result();
	}
	
	public function duplicate_date($date)
	{
		$this->db->select('user_leave_days.date');
		$this->db->from('user_leave');
		$this->db->where('user_leave.user_id',$this->session->userdata('user_id'));
		$this->db->join('user_leave_days','user_leave.id = user_leave_days.user_leave_id');
		$this->db->where('user_leave_days.date',$date);
		return $this->db->get()->num_rows();
		
		//return $this->db->get_where('user_leave', array('user_id' => $this->session->userdata('user_id'),'date'=>$date))->num_rows();
	}
	
	public function delete($id)
	{
		$this->db->delete('user_leave', array('id' => $id));
		$this->db->delete('user_leave_days', array('user_leave_id' => $id)); 
		$this->db->close();
	}
	
	public function delete_particular($pid)
	{		
		$this->db->delete('user_leave_days', array('id' => $pid));
		$this->db->close();
	}
	
	
	public function edit($edit_id)
	{
		return $this->db->get_where('user_leave',array('id'=>$edit_id))->row_array();
		$this->db->close();
	}
	
	public function update($data,$edit_id)
	{
		$this->db->update('user_leave',$data,array('id'=>$edit_id));
		$this->db->close();
	}
	
}
